package LC.nums.binarySearch;

public class LC34 {
    public int[] searchRange(int[] nums, int target) {
        int temp = binarySearch(nums, target);
        int[] ints = new int[]{-1, -1};

        if (temp == -1) {
            return ints;
        } else if (nums.length <= 2) {
            //pass
        } else if (nums[temp - 1] == target) {
            ints[0] = temp - 1;
            ints[1] = temp;
            return ints;
        } else if (nums[temp + 1] == target) {
            ints[0] = temp;
            ints[1] = temp + 1;
            return ints;
        }
        return ints;
    }

    /**
     * @param nums
     * @param target
     * @return
     */
    public int binarySearch(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int middle = left + ((right - left) / 2);
            if (nums[middle] < target) {
                left = middle + 1;
            } else if (nums[middle] > target) {
                right = middle - 1;
            } else {
                return middle;
            }
        }
        return -1;
    }
}

